package web;
import pojo.SmbmsRole;
import pojo.SmbmsUser;
import service.SmbmsRoleService;
import service.SmbmsUserService;
import service.impl.SmbmsRoleServiceImpl;
import service.impl.SmbmsUserServiceImpl;
import utils.Constants;
import utils.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet(value = "/userListServlet")
public class SmbmsUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//调用业务层实现
        SmbmsUserService smbmsUserService=new SmbmsUserServiceImpl();
        SmbmsRoleService smbmsRoleService=new SmbmsRoleServiceImpl();
//用户名从页面传值 name属性获取值
        String username=req.getParameter("username");
//从界面获取角色
        String roleId=req.getParameter("userRole");
        int userRole=0;
        if(roleId != null && !roleId.equals("")){
            userRole=Integer.parseInt(roleId);
        }
        System.out.println(userRole+"=========================");
        if(username ==null){
            username="";
        }
//获取当前页
        String pageIndex=req.getParameter("pageIndex");
        int currentPage=1;
        if(pageIndex !=null){
            currentPage=Integer.parseInt(pageIndex);
        }
        int pageSize= Constants.pageSize;
//获取总数量
        int smbmsUserCount = 0;
        try {
            smbmsUserCount = smbmsUserService.getSmbmsUserCount(username,userRole);
//获取总页数
            PageSupport pageSupport=new PageSupport();
//当前页
            pageSupport.setCurrentPageNo(currentPage);
//页面容量
            pageSupport.setPageSize(pageSize);
//总数量
            pageSupport.setTotalCount(smbmsUserCount);
//获取总页数
            int totalPageCount=pageSupport.getTotalPageCount();
//控制首页和尾页
            if(currentPage<1){
                currentPage=1;
            }else if(currentPage > totalPageCount){
                currentPage=totalPageCount;
            }
            List<SmbmsUser> smbmsUsers = smbmsUserService.queryList(username,userRole,currentPage,pageSize);
            if(smbmsUsers.size()>0){
//把数据放在request作用域中
                req.setAttribute("pages",pageSupport);
                req.setAttribute("username",username);
                req.setAttribute("list",smbmsUsers);
            }
            List<SmbmsRole> smbmsRoles = smbmsRoleService.queryRole();
            if(smbmsRoles.size()>0){
//把角色存在request域中
                req.setAttribute("userRole",userRole);
                req.setAttribute("roleList",smbmsRoles);
            }
//转发
            req.getRequestDispatcher("userList.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




//2021.01.12
//@Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
//            throws ServletException, IOException {
////用户名从页面传值 name属性获取值
//        String username=req.getParameter("username");
////从界面获取角色
//        String roleId=req.getParameter("userRole");
//        int userRole=0;
//        if(roleId != null && !roleId.equals("")){
//            userRole=Integer.parseInt(roleId);
//        }
//        System.out.println(userRole+"=========================");
//        if(username ==null){
//            username="";
//        }
////调用业务层实现
//        SmbmsUserService smbmsUserService=new SmbmsUserServiceImpl();
//        SmbmsRoleService smbmsRoleService=new SmbmsRoleServiceImpl();
//        List<SmbmsUser> smbmsUsers = null;
//        try {
//            smbmsUsers = smbmsUserService.queryList(username,userRole);
//            if(smbmsUsers.size()>0){
////把数据放在request作用域中
//                req.setAttribute("username",username);
//                req.setAttribute("list",smbmsUsers);
//            }
//            List<SmbmsRole> smbmsRoles = smbmsRoleService.queryRole();
//            if(smbmsRoles.size()>0){
////把角色存在request域中
//                req.setAttribute("userRole",userRole);
//                req.setAttribute("roleList",smbmsRoles);
//            }
////转发
//            req.getRequestDispatcher("userList.jsp").forward(req,resp);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//2021.01.11
//import pojo.SmbmsRole;
//import pojo.SmbmsUser;
//import service.SmbmsRoleService;
//import service.SmbmsUserService;
//import service.impl.SmbmsRoleServiceImpl;
//import service.impl.SmbmsUserServiceImpl;
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.List;
//@WebServlet(value = "/userListServlet")
//public class SmbmsUserServlet extends HttpServlet {
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
//            ServletException, IOException {
//        this.doPost(req, resp);
//    }
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String username=req.getParameter("username");
//        if(username ==null){
//            username="";
//        }
//        //调用业务层实现
//        SmbmsUserService smbmsUserService=new SmbmsUserServiceImpl();
//
//        SmbmsRoleService smbmsRoleService=new SmbmsRoleServiceImpl();
//
//        List<SmbmsUser>smbmsUsers = null;
//        try {
//            smbmsUsers = smbmsUserService.queryList(username);
//            if(smbmsUsers.size()>0) {
//                //把数据放在request作用域中
//                req.setAttribute("list", smbmsUsers);
//            }
//            List<SmbmsRole>smbmsRoles = smbmsRoleService.queryRole();
//            if (smbmsRoles.size()>0) {
//                req.setAttribute("roleList", smbmsRoles);
//            }
//        //转发
//                req.getRequestDispatcher("userList.jsp").forward(req,resp);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}